#include "ProjectHandle.h"

ProjectHandle::ProjectHandle(QObject *parent) : QObject(parent)
{
    mMdlFilePathList = new QList<QString>();
    mEventList=new QList<Event *>();
    mLibraryList=new QList<QString >();
    mHardwareCondition=new QMap<QString,QString>();
    mSoftwareEnvironment=new  QMap<QString,QString>();
    mTestifyPathList=new QList<QString>();
    mSupportivePathList=new QList<QString>();
}

void ProjectHandle::addEvent(Event *PEvent){
    if(PEvent==NULL) return;
    this->mEventList->append(PEvent);
}

void ProjectHandle::addHardwareCondition(QString Key, QString Value){
    if(Key.isEmpty()||Value.isEmpty()) return;
    this->mHardwareCondition->insert(Key,Value);
}

void ProjectHandle::addLibrary(QString LibraryPath){
    if(LibraryPath.isEmpty()) return ;
    this->mLibraryList->append(LibraryPath);
}

void ProjectHandle::addMdlFilePath(QString MdlFilePath){
    if(MdlFilePath.isEmpty()) return ;
    this->mMdlFilePathList->append(MdlFilePath);
}


void ProjectHandle::addSoftwareEnvironment(QString Key, QString Value){
    if(Key.isEmpty()||Value.isEmpty()) return ;
    this->mSoftwareEnvironment->insert(Key,Value);
}

void ProjectHandle::addSupportive(QString Supportive){
    if(Supportive.isEmpty()) return;
    this->mSupportivePathList->append(Supportive);
}

void ProjectHandle::addTestifyPath(QString TestifyPath){
    if(TestifyPath.isEmpty()) return ;
    this->mTestifyPathList->append(TestifyPath);
}

void ProjectHandle::setCurrentMdlIndex(int CurrentMdlIndex){
    if(CurrentMdlIndex<0) return ;    //maybe not suitable
    this->mCurrentMdlIndex = CurrentMdlIndex;
}

void ProjectHandle::setLicense(QString LicenseName){
    if(LicenseName.isEmpty()) return ;
    this->mLicenseName=LicenseName;
}

void ProjectHandle::setModelFilePath(QString ModelFilePath){
    if(ModelFilePath.isEmpty()) return ;
    this->mModelFilePath = ModelFilePath;
}

void ProjectHandle::setModelFileRelativePath(QString ModelFileRelativePath)
{
    if(ModelFileRelativePath.isEmpty()) return ;
    this->mModelFileRelativePath = ModelFileRelativePath;
}

void ProjectHandle::setInputParameter(QString InputParameter)
{
    if(InputParameter.isEmpty()) return ;
    this->mInputParameter = InputParameter;
}

void ProjectHandle::setModelType(QString ModelType){
    if(ModelType.isEmpty()) return ;
    this->mModelType = ModelType;
}

void ProjectHandle::setXmlPath(QString XmlPath){
    if(XmlPath.isEmpty()) return;
    this->mXmlPath = XmlPath;
}

//get

int ProjectHandle::getCurrentMdlIndex(){
    return this->mCurrentMdlIndex;
}

Event * ProjectHandle::getEvent(int index){
    if(index<0||index>this->mEventList->count()) return NULL;
    return this->mEventList->at(index);
}

int ProjectHandle::getEventCount(){
    return this->mEventList->count();
}

QMap<QString,QString> *ProjectHandle::getHardwareCondition(){
    return this->mHardwareCondition;
}

QString ProjectHandle::getLibrary(int index){
    if(index<0||index>this->mLibraryList->count()) return "";
    return this->mLibraryList->at(index);
}

int ProjectHandle::getLibraryCount(){
    return this->mLibraryList->count();
}

QString ProjectHandle::getLicenseName(){
    return this->mLicenseName;
}

int ProjectHandle::getMdlFileCount(){
    return this->mMdlFilePathList->count();
}

QString ProjectHandle::getMdlFilePath(int index){
    if(index<0||index>this->mMdlFilePathList->count()) return "";
    return this->mMdlFilePathList->at(index);

}

QString ProjectHandle::getModelFilePath(){
    return this->mModelFilePath;
}

QString ProjectHandle::getModelFileRelativePath()
{
    return this->mModelFileRelativePath;
}

QString ProjectHandle::getInputParameter()
{
    return this->mInputParameter;
}

QString ProjectHandle::getModelType(){
    return this->mModelType;
}

QMap<QString,QString> *ProjectHandle::getSoftwareEnvironment(){
    return this->mSoftwareEnvironment;
}

QString ProjectHandle::getSupportivePath(int index){
    if(index<0||index>this->mSupportivePathList->count()) return "";
    return this->mSupportivePathList->at(index);
}

int ProjectHandle::getSupportivePathCount(){
    return this->mSupportivePathList->count();
}

QString ProjectHandle::getTestifyPath(int index){
    if(index<0||index>this->mTestifyPathList->count()) return "";
    return this->mTestifyPathList->at(index);
}

int ProjectHandle::getTestifyPathCount(){
    return this->mTestifyPathList->count();
}

QString ProjectHandle::getXmlPath(){
    return this->mXmlPath;
}

bool ProjectHandle::LoadFromFile(QString XmlPath){
    if(XmlPath.isEmpty()) return false;

    QDomDocument m_doc("mydocument");
    QFile file( XmlPath );
    if(!file.exists()){
        qWarning()<<XmlPath<<"not exist."<<endl;
        return false;
    }
    if( !file.open( QFile::ReadOnly | QFile::Text  ) )
    {
        qDebug() << QObject::tr("error::ParserXML->OpenXmlFile->file.open->%s\n") << XmlPath;
        return false;
    }
    if( !m_doc.setContent( &file ) )
    {
        qDebug() << QObject::tr("error::ParserXML->OpenXmlFile->doc.setContent\n") << XmlPath;
        file.close();
        return false;
    }
    file.close();
    this->setXmlPath(XmlPath);
    //begin read xml
    QDomElement root = m_doc.documentElement();

   if(root.tagName()=="RootElem"){
       QDomElement ModelListNode =  root.firstChildElement("ModelList");
       if(!ModelListNode.isNull()){
           if(!ModelListNode.attribute("currentIndex").isEmpty()){
               this->setCurrentMdlIndex((ModelListNode.attribute("currentIndex").toInt()));
           }
           QDomNodeList MdlList = ModelListNode.childNodes();
           for(int i=0;i<MdlList.count();i++){
               //qWarning()<<MdlList.at(i).toElement().text()<<endl;
               this->addMdlFilePath(MdlList.at(i).toElement().text());
           }
       }

       //ModelEntrance
       QDomElement ModelEntrance = root.firstChildElement("ModelEntrance");
       if(!ModelEntrance.isNull()){
           QDomElement ModelTypeNode = ModelEntrance.firstChildElement("ModelType");
           if(!ModelTypeNode.isNull()){
               this->setModelType(ModelTypeNode.text());
           }

           QDomElement ModelPathNode = ModelEntrance.firstChildElement("ModelPath");
           if(!ModelPathNode.isNull()){
               this->setModelFilePath(ModelPathNode.text());
           }

           QDomElement ModelRelativePathNode = ModelEntrance.firstChildElement("ModelRelativePath");
           if(!ModelRelativePathNode.isNull()){
               this->setModelFileRelativePath(ModelRelativePathNode.text());
           }

           QDomElement InputParameterNode = ModelEntrance.firstChildElement("InputParameter");
           if(!InputParameterNode.isNull()){
               this->setInputParameter(InputParameterNode.text());
           }
       }

       //EventList
       QDomElement EventListNode = ModelEntrance.firstChildElement("EventList");
       if(!EventListNode.isNull()){
           QDomNodeList EventsNode = EventListNode.childNodes();
           for(int i=0;i<EventsNode.count();i++){
               Event *OneEvent = new Event();
               OneEvent->setEventName(EventsNode.at(i).toElement().attribute("name"));
               if(!EventsNode.at(i).firstChildElement("MethodFilePath").isNull()){
                   OneEvent->setMethodFilePath(EventsNode.at(i).firstChildElement("MethodFilePath").text());
               }
               if(!EventsNode.at(i).firstChildElement("DataRelativePath").isNull()){
                   OneEvent->setDataFilePath(EventsNode.at(i).firstChildElement("DataRelativePath").text());
               }
               this->addEvent(OneEvent);
           }
       }

       //ModelLibrary
       QDomElement ModelLibraryNode = root.firstChildElement("ModelLibrary");
       if(!ModelLibraryNode.isNull()){
           QDomElement LibraryListNode = ModelLibraryNode.firstChildElement("LibraryList");
           if(!LibraryListNode.isNull()){
               QDomNodeList LibPathsNode = LibraryListNode.childNodes();
               for(int i=0;i<LibPathsNode.count();i++){
                   if(!LibPathsNode.at(i).isNull()){
                       this->addLibrary(LibPathsNode.at(i).toElement().text());
                   }
               }
           }
       }

       //HardwareCondition
       QDomElement HardwareConditionNode = root.firstChildElement("HardwareCondition");
       if(!HardwareConditionNode.isNull()){
           QDomElement HardwareListNode = HardwareConditionNode.firstChildElement("ConditionList");
           if(!HardwareListNode.isNull()){
               QDomNodeList ConditionsNode = HardwareListNode.childNodes();
               for(int i=0;i<ConditionsNode.count();i++){
                   this->addHardwareCondition(ConditionsNode.at(i).toElement().attribute("name"),
                                              ConditionsNode.at(i).toElement().text());
               }
           }
       }

       //SoftwareEnvironment
       QDomElement SoftwareEnvironmentNode = root.firstChildElement("SoftwareEnvironment");
       if(!SoftwareEnvironmentNode.isNull()){
           QDomElement SoftwareListNode = SoftwareEnvironmentNode.firstChildElement("ConditionList");
           if(!SoftwareListNode.isNull()){
               QDomNodeList SConditionsNode = SoftwareListNode.childNodes();
               for(int i=0;i<SConditionsNode.count();i++){
                   this->addSoftwareEnvironment(SConditionsNode.at(i).toElement().attribute("name"),
                                              SConditionsNode.at(i).toElement().text());
               }
           }
       }

       //TestifyData
       QDomElement TestifyDataNode = root.firstChildElement("TestifyData");
       if(!TestifyDataNode.isNull()){
           QDomElement DataListNode = TestifyDataNode.firstChildElement("DataList");
           if(!DataListNode.isNull()){
               QDomNodeList DataPathsNode = DataListNode.childNodes();
               for(int i=0;i<DataPathsNode.count();i++){
                   this->addTestifyPath(DataPathsNode.at(i).toElement().text());
               }
           }
       }

       //SupportiveResource
        QDomElement SupportiveResourceNode = root.firstChildElement("SupportiveResource");
        if(!SupportiveResourceNode.isNull()){
            QDomElement ResourceListNode = SupportiveResourceNode.firstChildElement("ResourceList");
            if(!ResourceListNode.isNull()){
                QDomNodeList ResourcePaths = ResourceListNode.childNodes();
                for(int i=0;i<ResourcePaths.count();i++){
                    this->addSupportive(ResourcePaths.at(i).toElement().text());
                }
            }
        }

        //License
        QDomElement LicenseNode=root.firstChildElement("License");
        if(!LicenseNode.isNull()){
            this->setLicense(LicenseNode.text());
        }
   }else {
       qWarning()<<"not the right file."<<endl;
       return false;
   }



    return true;
}


bool ProjectHandle::SaveToFile(QString XmlPath){
    QDomDocument m_doc("mydocument");

    //create xml

    //root
    QDomElement rootNode = m_doc.createElement("RootElem");
    m_doc.appendChild(rootNode);
    rootNode.setAttribute("name","UIConfig");

    //ModelList
    QDomElement ModelListNode = m_doc.createElement("ModelList");
    ModelListNode.setAttribute("currentIndex",this->getCurrentMdlIndex ());                //currentIndex
    rootNode.appendChild(ModelListNode);

    for(int i=0;i<this->getMdlFileCount();i++){
        QDomElement MdlFilePathMode = m_doc.createElement("MdlFilePath");
        ModelListNode.appendChild(MdlFilePathMode);
        MdlFilePathMode.setAttribute("index",i);                     // 0
        QDomText MdlPathText = m_doc.createTextNode(this->getMdlFilePath (i));  // D:/text.mdl
        MdlFilePathMode.appendChild(MdlPathText);
    }


    //ModelEntrance
    QDomElement ModelEntranceNode = m_doc.createElement("ModelEntrance");
    rootNode.appendChild(ModelEntranceNode);

    //ModelType
    QDomElement ModelTypeNode = m_doc.createElement(tr("ModelType"));
    ModelEntranceNode.appendChild(ModelTypeNode);
    QDomText ModelTypeText = m_doc.createTextNode(this->getModelType ());  //
    ModelTypeNode.appendChild(ModelTypeText);

    //ModelPath
    QDomElement ModelPathNode = m_doc.createElement(tr("ModelPath"));
    ModelEntranceNode.appendChild(ModelPathNode);
    QDomText ModelPathText = m_doc.createTextNode(this->getModelFilePath());  //
    ModelPathNode.appendChild(ModelPathText);

    //ModelRelativePath
    QDomElement ModelRelativePathNode = m_doc.createElement(tr("ModelRelativePath"));
    ModelEntranceNode.appendChild(ModelRelativePathNode);
    QDomText ModelRelativePathText = m_doc.createTextNode(this->getModelFileRelativePath());
    ModelRelativePathNode.appendChild(ModelRelativePathText);

    //InputParameter
    QDomElement InputParameterNode = m_doc.createElement(tr("InputParameter"));
    ModelEntranceNode.appendChild(InputParameterNode);
    QDomText InputParameterText = m_doc.createTextNode(this->getInputParameter());
    InputParameterNode.appendChild(InputParameterText);


    //EventList
    QDomElement EventListNode =m_doc.createElement (tr("EventList"));
    ModelEntranceNode.appendChild (EventListNode);

    for(int i=0;i<this->getEventCount ();i++){
        QDomElement OneEventNode = m_doc.createElement (tr("Event"));  //for event
        OneEventNode.setAttribute ("name",this->getEvent (i)->getEventName ());   //
        EventListNode.appendChild (OneEventNode);

        QDomElement MethodFilePathNode = m_doc.createElement (tr("MethodFilePath"));
        OneEventNode.appendChild (MethodFilePathNode);
        QDomText MethodsText = m_doc.createTextNode (this->getEvent (i)->getMethodFilePath ()); //
        MethodFilePathNode.appendChild (MethodsText);

        QDomElement DataRelativePathNode = m_doc.createElement (tr("DataRelativePath"));
        OneEventNode.appendChild (DataRelativePathNode);
        QDomText RelativePathText = m_doc.createTextNode (this->getEvent (i)->getDataFilePath ()); //
        DataRelativePathNode.appendChild (RelativePathText);
    }


    //ModelLibrary
    QDomElement ModelLibraryNode = m_doc.createElement (tr("ModelLibrary"));
    rootNode.appendChild (ModelLibraryNode);

    QDomElement LibraryListNode = m_doc.createElement (tr("LibraryList"));
    ModelLibraryNode.appendChild (LibraryListNode);
    for(int i=0;i<this->getLibraryCount ();i++){
        QDomElement LibPathNode = m_doc.createElement (tr("LibPath")); //for libPath
        LibraryListNode.appendChild (LibPathNode);
        QDomText LibText = m_doc.createTextNode (this->getLibrary (i)); //
        LibPathNode.appendChild (LibText);
    }


    //HardwareCondition
    QDomElement HardwareConditionNode = m_doc.createElement (tr("HardwareCondition"));
    rootNode.appendChild (HardwareConditionNode);

    QDomElement ConditionListNode = m_doc.createElement (tr("ConditionList"));
    HardwareConditionNode.appendChild (ConditionListNode);



    QMapIterator<QString, QString> HardwareMap(*this->getHardwareCondition ());
    while (HardwareMap.hasNext()) {
        HardwareMap.next();
        QDomElement ConditionNode = m_doc.createElement (tr("Condition")); //for condition
        ConditionListNode.appendChild (ConditionNode);
        ConditionNode.setAttribute ("name",HardwareMap.key ());
        QDomText ConditionText = m_doc.createTextNode (HardwareMap.value ());
        ConditionNode.appendChild (ConditionText);
    }


    //SoftwareEnvironment
    QDomElement SoftwareEnvironmentNode = m_doc.createElement (tr("SoftwareEnvironment"));
    rootNode.appendChild (SoftwareEnvironmentNode);

    QDomElement SoftwareConditionListNode = m_doc.createElement (tr("ConditionList"));
    SoftwareEnvironmentNode.appendChild (SoftwareConditionListNode);

    QMapIterator<QString, QString> SoftwareMap(*this->getSoftwareEnvironment ());
    while (SoftwareMap.hasNext()) {
        SoftwareMap.next();
        QDomElement SoftwareConditionNode = m_doc.createElement (tr("Condition")); //for condition
        SoftwareConditionListNode.appendChild (SoftwareConditionNode);
        SoftwareConditionNode.setAttribute ("name",SoftwareMap.key ());
        QDomText SoftwareConditionText = m_doc.createTextNode (SoftwareMap.value ());
        SoftwareConditionNode.appendChild (SoftwareConditionText);

    }

    //TestifyData
    QDomElement TestifyDataNode = m_doc.createElement (tr("TestifyData"));
    rootNode.appendChild (TestifyDataNode);

    QDomElement DataListNode = m_doc.createElement (tr("DataList"));
    TestifyDataNode.appendChild (DataListNode);

    for(int i=0;i<this->getTestifyPathCount ();i++){
        QDomElement DataPathNode = m_doc.createElement (tr("DataPath")); //for testify
        DataListNode.appendChild (DataPathNode);
        QDomText DataPathText = m_doc.createTextNode (this->getTestifyPath (i)); //
        DataPathNode.appendChild (DataPathText);
    }


    //SupportiveResource
    QDomElement SupportiveNode = m_doc.createElement (tr("SupportiveResource"));
    rootNode.appendChild (SupportiveNode);

    QDomElement ResourceListNode = m_doc.createElement (tr("ResourceList"));
    SupportiveNode.appendChild (ResourceListNode);

    for(int i=0;i<this->getSupportivePathCount ();i++){
        QDomElement ResourcePathNode = m_doc.createElement (tr("ResourcePath")); //for supportive
        ResourceListNode.appendChild (ResourcePathNode);
        QDomText ResourcePathText = m_doc.createTextNode (this->getSupportivePath (i)); //
        ResourcePathNode.appendChild (ResourcePathText);

    }

    //License
    QDomElement LicenseNode = m_doc.createElement (tr("License")); //for License
    rootNode.appendChild (LicenseNode);
    QDomText LicenseText = m_doc.createTextNode (this->getLicenseName ()); //
    LicenseNode.appendChild (LicenseText);


    //save to xml file
    QFile filexml(XmlPath);
    if( !filexml.open( QFile::WriteOnly | QFile::Truncate) ){
        qWarning()<<"error::ParserXML->writeOperateXml->file.open\n"<<endl;
        return false;
    }
    QTextStream ts(&filexml);
    ts.reset();
    ts.setCodec("utf-8");
    m_doc.save(ts, 4, QDomNode::EncodingFromTextStream);
    filexml.close();
    return true;

}















